package com.jwdf.uda.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.jwdf.uda.R;
import com.jwdf.uda.utils.UdaUtils;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/5/11.
 */
public class GridImageLayout extends FrameLayout {

    private RoundedImageView[] mImageViews;
    private TextView mThumbNum;
    private FrameLayout mFlThumbNum;
    private OnImageItemClickListener mImageItemClickListener;

    public GridImageLayout(Context context) {
        super(context);
        initViews();
    }

    public GridImageLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews();
    }

    private void initViews() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_gridimage, this, true);
        mThumbNum = (TextView) findViewById(R.id.tvThumbNum);
        mFlThumbNum=(FrameLayout)findViewById(R.id.fl_thumb);
        mImageViews = new RoundedImageView[9];
        for (int i = 0; i < mImageViews.length; i++) {
            int resid = UdaUtils.getId(getContext(), "img_" + i);
            if (resid > 0) {
                mImageViews[i] = (RoundedImageView) findViewById(resid);
                mImageViews[i].setTag(R.id.tag_index, i);
                mImageViews[i].setCornerRadiusDimen(R.dimen.riv_corner_radius);
                mImageViews[i].setOnClickListener(mClickListener);
            }
        }
    }

    private OnClickListener mClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int index = (int) v.getTag(R.id.tag_index);
            if (mImageItemClickListener != null) {
                mImageItemClickListener.onImageClick(index, mImageViews[index]);
            }
        }
    };

    public void setOnImageItemClickListener(OnImageItemClickListener listener) {
        this.mImageItemClickListener = listener;
    }


    public void setImage(List<String> data) {
        for (int i = 0; i < mImageViews.length; i++) {
            if (i < data.size()) {
                ImageLoader.getInstance().displayImage(data.get(i), mImageViews[i]);
                mImageViews[i].setVisibility(VISIBLE);
            } else if (i > 0 && i % 3 != 0 && mImageViews[i - 1].getVisibility() != View.GONE) {
                mImageViews[i].setVisibility(INVISIBLE);
            } else {
                mImageViews[i].setVisibility(GONE);
            }
        }
    }

    public void setThumbNum(int num) {
        mFlThumbNum.setVisibility(VISIBLE);
        mThumbNum.setText(num + "张");
    }

    public void setThumbNumGone() {
        mFlThumbNum.setVisibility(GONE);
    }

    public interface OnImageItemClickListener {
        void onImageClick(int index, ImageView imageView);
    }

}
